﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Reflection;
using System.IO;
using System.Security.Cryptography;

namespace RDPCommon
{
    public static class Util
    {
        //日志文件名默认 exe.log 
        static string m_LogFile = String.Format("{0}.log", AppDomain.CurrentDomain.FriendlyName);
        //日志文件最大大小,若超过则改名字另存
        static int m_Max = 5000000; //约5M

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="log"></param>
        public static void SaveLog(string log)
        {
            System.IO.StreamWriter sw = null;
            try
            {
                if (System.IO.File.Exists(m_LogFile))
                {
                    System.IO.FileInfo fi = new System.IO.FileInfo(m_LogFile);
                    if (fi.Length > m_Max)
                    {
                        string targFile = fi.Name.Replace(fi.Extension, "") + "." + System.DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + fi.Extension;
                        fi.MoveTo(targFile);
                    }
                }

                sw = new System.IO.StreamWriter(m_LogFile, true);
                sw.WriteLine(String.Format("---{0}---", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:dd")));
                sw.WriteLine(log);
                sw.WriteLine(System.Environment.NewLine);
                sw.Flush();
            }
            catch
            {
            }
            finally
            {
                if (sw != null)
                    sw.Close();
            }
        }

        /// <summary>
        /// 返回异常的消息,因有可能嵌套异常,故要递归
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static string GetExceptionMsg(Exception ex)
        {
            string rtn = "";
            if (ex != null)
            {
                rtn += ex.Message + System.Environment.NewLine;
                rtn += ex.StackTrace + System.Environment.NewLine;
                if (ex.InnerException != null)
                    rtn = GetExceptionMsg(ex.InnerException);
            }
            return rtn;
        }

        /// <summary>
        /// 返回当前设置IP
        /// </summary>
        /// <returns></returns>
        public static string GetIpAddress()
        {
            IPHostEntry ipHE = Dns.GetHostEntry(Dns.GetHostName());
            if (ipHE!=null && ipHE.AddressList.Length >0)
                return ipHE.AddressList[0].ToString();
            return "";
        }

        /// <summary>
        /// 返回当前设置Name
        /// </summary>
        /// <returns></returns>
        public static string GetPCName()
        {
            return Dns.GetHostName();
        }

        /// <summary>
        /// 警告提示
        /// </summary>
        /// <param name="msg"></param>
        public static void ShowWarningDialog(string msg)
        {
            MessageBox.Show(msg, "警告提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);           
        }

        /// <summary>
        /// 信息提示
        /// </summary>
        /// <param name="msg"></param>
        public static void ShowInfoDialog(string msg)
        {
            MessageBox.Show(msg, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// 错误提示
        /// </summary>
        /// <param name="msg"></param>
        public static void ShowErrorDialog(string msg)
        {
            MessageBox.Show(msg, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// 操作确认
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static DialogResult ShowConfigDialog(string msg)
        {
            return MessageBox.Show(msg, "操作确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        }

        /// <summary>
        /// 异常提示
        /// </summary>
        /// <param name="msg"></param>
        public static void ShowExceptionDialog(Exception ex)
        {
            frmException frm = new frmException(ex);
            frm.ShowDialog();
        }

        static string key = "rdp2010_^_^"; //可自行设定,至少8位

        /// <summary>
        /// DES 加密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string DesEncrypt(string input)
        {
            if (String.IsNullOrEmpty(input))
                return input;
            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
            byte[] keyIV = keyBytes;
            byte[] inputByteArray = Encoding.UTF8.GetBytes(input);

            DESCryptoServiceProvider provder = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provder.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray());
        }

        /// <summary>
        /// DES 解密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string DesDecrypt(string input)
        {
            if (String.IsNullOrEmpty(input))
                return input;
            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
            byte[] keyIV = keyBytes;
            byte[] inputByteArray = Convert.FromBase64String(input);

            DESCryptoServiceProvider provder = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provder.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(mStream.ToArray());

        }
    }
}
